﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Bussiness.Manager;
using Bussiness.Entity;
using WB.Library;
public partial class Admin_Category_AddCategory : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsPostBack)
        {
            hidCatId.Value = Request["id"];
            if (Request["control"] == "addncat")
                CatType = "NEWS";
            else
                CatType = "PROD";
            LoadParent();
            LoadDetail();
            if (CategoryInfoId != Guid.Empty)
            {
                btnUpdate.Visible = true;
            }
            else
                btnUpdate.Visible = false;
        }
    }

    protected void LoadDetail()
    {
        if (CategoryId != Guid.Empty)
        {
            CmsCategoryInfosEntity objCond = new CmsCategoryInfosEntity();
            objCond.CategoryId = CategoryId;
            objCond.Lang = Language;
            DataTable tbl = CmsCategoryInfosManager.CreateInstant().SelectWithCondition(objCond);
            if (tbl.Rows.Count > 0)
            {
                hidCatInfo.Value = tbl.Rows[0]["Id"].ToString();
                txtCatName.Text = tbl.Rows[0]["CatName"].ToString();
                ddlParent.SelectedValue = tbl.Rows[0]["CategoryId"].ToString();
            }
        }
    }

    /// <summary>
    /// Loai category : NEWS, PROD
    /// </summary>
    public string Language
    {
        get { return hidLang.Value; }
        set
        {
            hidLang.Value = value;
            switch (value)
            {
               
                case "en-US": lblTitle.Text = "CATEGORY DETAIL";
                    break;
                default: lblTitle.Text = "CHI TIẾT CATEGORY";
                    break;
            }
        }
    }
    public Guid CategoryId
    {
        get {
            if (FGuid.IsGuid(hidCatId.Value))
                return new Guid(hidCatId.Value);
            return Guid.Empty;
        }
        set
        {
            hidCatId.Value = value.ToString();
        }
    }
    public Guid CategoryInfoId
    {
        get
        {
            if (FGuid.IsGuid(hidCatInfo.Value))
                return new Guid(hidCatInfo.Value);
            return Guid.Empty;
        }
        set
        {
            hidCatInfo.Value = value.ToString();
        }
    }
    /// <summary>
    /// Loai category : NEWS, PROD
    /// </summary>
    public string CatType
    {
        set
        {
            hidCatType.Value = value;
            switch (hidCatType.Value)
            {
                case "NEWS": lblTitle.Text = "THÊM DANH MỤC TIN TỨC";
                    break;
                case "PROD": lblTitle.Text = "THÊM DANH MỤC SẢN PHẨM";
                    break;

            }
        }
        get { return hidCatType.Value; }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        
            Insert();
            Page_Load(sender, e);
            txtCatName.Text = "";
       
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (CategoryId != Guid.Empty)
        {
            Update();
            CustomValidator1.ErrorMessage = "Đã lưu dữ liệu.";
            CustomValidator1.IsValid = false;
        }
        else
        {
            CustomValidator1.ErrorMessage = "Không tìm thấy dữ liệu";
            CustomValidator1.IsValid = false;
        }
    }
    protected void Update()
    {
        CmsCategoryInfosEntity obj = new CmsCategoryInfosEntity();
        obj.Id = CategoryInfoId;
        obj.CatName = txtCatName.Text;
        CmsCategoryInfosManager.CreateInstant().Update(obj);
        CustomValidator1.ErrorMessage = "Đã lưu dữ liệu";
        CustomValidator1.IsValid = false;

        if (FGuid.IsGuid(ddlParent.SelectedValue))
        {
            CmsCategoryEntity objCat = new CmsCategoryEntity();
            objCat.Id = CategoryId;
            objCat.ParentId = new Guid(ddlParent.SelectedValue);
            CmsCategoryManager.CreateInstant().Update(objCat);
        }
        Response.Redirect(this.Request.RawUrl);
    }

    protected void LoadParent()
    {
      //  Response.Write("cattype:" + CatType + "lang:" + Language);
        DataTable tbl = CmsCategoryManager.CreateInstant().GetCategoryTreeByLang(CatType, Language);
        ddlParent.Items.Clear();
        ddlParent.Items.Add(new ListItem("--Không chọn--",""));
        ddlParent.AppendDataBoundItems = true;
        ddlParent.DataSource = tbl;
        ddlParent.DataTextField = "CatName";
        ddlParent.DataValueField = "CategoryId";
        ddlParent.DataGroupField = "ParentName";
        ddlParent.DataBind();
    }

    protected void Insert()
    { 
        //insert CmsCategory
        CmsCategoryEntity objCat = new CmsCategoryEntity();
        objCat.CatType = CatType;
        objCat.Id = Guid.NewGuid();
        if(FGuid.IsGuid(ddlParent.SelectedValue))
            objCat.ParentId = new Guid(ddlParent.SelectedValue);
        CmsCategoryManager.CreateInstant().Insert(objCat);

        CategoryId = objCat.Id;
        
        //insert CmsCategoryInfo
        CmsCategoryInfosEntity objCatInfo = new CmsCategoryInfosEntity();
        objCatInfo.CategoryId = objCat.Id;
        objCatInfo.Lang = Language;
        objCatInfo.CatName = txtCatName.Text;
        CmsCategoryInfosManager.CreateInstant().Insert(objCatInfo);

        CmsCategoryInfosEntity objCatInfo2 = new CmsCategoryInfosEntity();
        objCatInfo2.CategoryId = objCat.Id;
        objCatInfo2.Lang = Language.ToLower() =="vi-vn"?"en-US":"vi-VN";
        CmsCategoryInfosManager.CreateInstant().Insert(objCatInfo2);
        if (CatType == "NEWS")
            Response.Redirect("?control=addncat&id=" + objCat.Id);
        else
            Response.Redirect("?control=addprocat&id=" + objCat.Id);
       
        

    }

    protected void btnList_Click(object sender, EventArgs e)
    {
        if (CatType == "NEWS")        
            Response.Redirect("?control=ncat");
        else
            Response.Redirect("?control=procat");
    }
}
